﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>D3分段专题图</title>
    <script type="text/javascript" include="jquery,bootstrap" src="../js/include-web.js"></script>
    <script type="text/javascript" include="d3,d3Layer" src="../../dist/include-leaflet.js"></script>
    <style type="text/css">
        .legendItemHeader,
        .legendItemValue {
            width: 120px;
            height: 18px;
            font-size: 14px;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript">
    var host = window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090";
    var map, resultLayer,
            baseUrl = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
            url = host + "/iserver/services/data-jingjin/rest/data";
    map = L.map('map', {
        preferCanvas: true,
        crs: L.CRS.EPSG4326,
        center: [39.79, 116.85],
        maxZoom: 18,
        zoom: 7
    });
    L.supermap.tiledMapLayer(baseUrl).addTo(map);
    query();

    var rangeStyles = [{
        start: 0,
        end: 0.02,
        color: 'rgb(253,226,202)'
    }, {
        start: 0.02,
        end: 0.04,
        color: 'rgb(250,206,156)'
    }, {
        start: 0.04,
        end: 0.06,
        color: 'rgb(240,156,66)'
    }, {
        start: 0.06,
        end: 0.1,
        color: 'rgb(208,119,11)'
    }, {
        start: 0.1,
        end: 0.2,
        color: 'rgb(148,83,5)'
    }];

    function query() {
        var sqlParam = new SuperMap.GetFeaturesBySQLParameters({
            queryParameter: {
                name: "Jingjin",
                attributeFilter: "SMID >=  -1"
            },
            datasetNames: ["Jingjin:BaseMap_R"],
            fromIndex: 0,
            toIndex: 200
        });
        L.supermap.featureService(url).getFeaturesBySQL(sqlParam, function (serviceResult) {
            var d3Layer = L.supermap.d3Layer(function (sel, proj) {
                var upd = sel.selectAll('path').data(serviceResult.result.features.features);
                upd.enter()
                        .append('path')
                        .attr('d', proj.pathFromGeojson)
                        .attr('fill', function (feature) {
                            var pop = parseFloat(feature.properties.POP_DENSITY99);
                            for (var i = 0; i < rangeStyles.length; i++) {
                                var rangeStyle = rangeStyles[i];
                                if (pop >= rangeStyle['start'] && pop <= rangeStyle['end']) {
                                    return rangeStyle['color'];
                                }
                            }
                        })
                        .attr('fill-opacity', '0.8')
            });
            d3Layer.addTo(map);
            initLegendView();
        });
    }

    //图例控件
    function initLegendView() {
        var legendView = L.control({position: 'bottomright'});
        legendView.onAdd = function () {
            this._div = L.DomUtil.create('div', 'panel panel-primary legend ');
            var title = "<div class='panel-heading'><h5 class='panel-title text-center'>图例</h5></div>";
            $(title + "<div class='panel-body text-center' ><table>" +
                    "<tr><td class='legendItemHeader'>人口密度(1999年)</td><td class='legendItemValue'>颜色</td></tr>" +
                    "<tr> <td class='legendItemHeader'>0    - 0.02</td> <td class='legendItemValue' style='background: #FDE2CA'></td></tr>" +
                    "<tr> <td class='legendItemHeader'>0.02 - 0.04</td> <td class='legendItemValue' style='background: #FACE9C'></td> </tr>" +
                    "<tr> <td class='legendItemHeader'>0.04 - 0.06</td> <td class='legendItemValue' style='background: #F09C42'></td> </tr>" +
                    "<tr> <td class='legendItemHeader'>0.06 - 0.1</td> <td class='legendItemValue' style='background: #D0770B'></td> </tr>" +
                    "<tr> <td class='legendItemHeader'>0.1  - 0.2</td> <td class='legendItemValue' style='background: #945305'></td> </tr>" +
                    "</table></div>"
            ).appendTo(this._div);
            handleMapEvent(this._div, this._map);
            return this._div;
        };
        legendView.addTo(map);
    }

    function handleMapEvent(div, map) {
        if (!div || !map) {
            return;
        }
        div.addEventListener('mouseover', function () {
            map.dragging.disable();
            map.scrollWheelZoom.disable();
            map.doubleClickZoom.disable();
        });
        div.addEventListener('mouseout', function () {
            map.dragging.enable();
            map.scrollWheelZoom.enable();
            map.doubleClickZoom.enable();
        });
    }
</script>
</body>
</html>